//
//  BDAddAddressTableViewCell.m
//  BadBoy
//
//  Created by wen on 2017/4/21.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDAddAddressTableViewCell.h"

@interface BDAddAddressTableViewCell ()

@property (nonatomic, strong) UITextField *textfield;
//选择手机联系人按钮
@property (nonatomic, strong) UIButton *contactButton;

@end

//新的地址cell
@implementation BDAddAddressTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    style = UITableViewCellStyleValue1;
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       
    }
    return self;
}

- (void)configeWithModel:(id)model addButtonBlock:(void(^)())addButtonBlock textfieldBlock:(void(^)(NSString *text))textBlock{
    self.textLabel.text = model[@"title"];
    self.textfield.text = model[@"subTitle"];
    self.textfield.userInteractionEnabled = YES;
    self.accessoryType = UITableViewCellAccessoryNone;
    self.contactButton.hidden = YES;
    if (model[@"needAdd"]) {
        self.contactButton.hidden = NO;
    }else if (model[@"notEdit"]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.textfield.userInteractionEnabled = NO;
    }
    [self.textfield.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (textBlock) {
            textBlock(self.textfield.text);
        }
    }];
    [[self.contactButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (addButtonBlock) {
            addButtonBlock();
        }
    }];
    
    if ([self.textLabel.text containsString:ASLocalizedString(@"号码")]) {
        self.textfield.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        self.textfield.keyboardType = UIKeyboardTypeDefault;
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.textLabel.font = [UIFont systemFontOfSize:14];
    self.textLabel.textColor = [UIColor colorWithHexString:@"#101010"];
    [self.textLabel sizeToFit];
    self.textLabel.left = 10;
    self.textLabel.centerY = self.height / 2.0;
    
    self.textfield.size = CGSizeMake(self.width - self.textLabel.right - 40, self.height);
    self.textfield.font = [UIFont systemFontOfSize:14];
    self.textfield.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
    self.textfield.right = self.width - 30;
    
    self.contactButton.size = CGSizeMake(20, 20);
    self.contactButton.right = self.width - 10;
    self.contactButton.centerY = self.height / 2.0;
    [self.contactButton setImage:[UIImage imageNamed:@"tianji"] forState:UIControlStateNormal];
}

- (UITextField *)textfield{
    if (!_textfield) {
        self.textfield = [[UITextField alloc] init];
        [self.contentView addSubview:_textfield];
    }
    return _textfield;
}

- (UIButton *)contactButton{
    if (!_contactButton) {
        self.contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _contactButton;
}

@end


